package com.nl.controller.web.controller;

import com.github.pagehelper.PageInfo;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.service.SmsBusiService;
import com.nl.controller.web.bean.SmsMgrInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("busi")
public class SmsBusiController {

    @Autowired
    private SmsBusiService smsBusiService;

    @ApiOperation(value = "新增短信项目", notes = "新增短信项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "项目名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cityId", value = "地市编码", required = true, dataType = "String")
    })
    @PostMapping("add")
    private @ResponseBody
    Response addBusi(
            @RequestParam("name") String name,
            @RequestParam("cityId") String cityId,
            @RequestParam("extId") String extId,
            @RequestParam("countyId") String countyId,
            @RequestParam Map<String, Object> paramMap
    ) {
        cityId = ("".equals(cityId) ? "25" : cityId);
        extId = ("".equals(extId) ? "" : extId);
        String id = smsBusiService.addBusi(name, cityId, extId,countyId);
        log.info(" 添加项目信息：{}", paramMap.toString());
        return ResponseUtil.sucess(id);
    }

    @ApiOperation(value = "查询项目信息", notes = "分页查询短信项目数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cityId", value = "地市编码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "proName", value = "项目名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "state", value = "项目状态", required = true, dataType = "String")
    })
    @PostMapping("find")
    public Response findBusi(
            @RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
            @RequestParam(value = "cityId", required = false) String cityId,
            @RequestParam(value = "mgrId", required = false) String mgrId,
            @RequestParam(value = "mgrName", required = false) String mgrName,
            @RequestParam(value = "state", required = false) String state,
            @RequestParam(value = "countyId", required = false) String countyId,
            @RequestParam Map<String, Object> paramMap
    ) {
        if ("".equals(cityId) || cityId == null) {
            throw new SmsException("请选择查询条件，开始查询");
        } else {
            PageInfo<SmsMgrInfo> pageInfo = smsBusiService.findBusi(page, limit, state, cityId, mgrId,mgrName,countyId);
            log.info(" 查询项目信息：{}", paramMap.toString());
            log.debug("查询项目信息返回参数：{}", pageInfo.getList().toString());
            return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
        }
    }

    @ApiOperation(value = "修改短信项目", notes = "修改短信项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "项目名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cityId", value = "地市编码", required = false, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "项目名称", required = false, dataType = "String"),
            @ApiImplicitParam(name = "state", value = "项目状态", required = false, dataType = "String"),
    })
    @PostMapping("modify")
    private @ResponseBody
    Response modifyBusi(
            @RequestParam(value = "id") String id,
            @RequestParam(value ="name",required =false) String name,
            @RequestParam(value ="cityId",required =false) String cityId,
            @RequestParam(value ="state",required =false) String state,
            @RequestParam Map<String, Object> paramMap

    ) {

        try {
            int result = smsBusiService.modifyMgr(id, name, cityId, state);
            log.info("修改项目信息:{}" +paramMap.toString());
            log.debug("返回信息："+result);
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("返回信息：");
        }
        return ResponseUtil.sucess("success");
    }

    @ApiOperation(value = "查询单个项目信息", notes = "查询单个项目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mgrId", value = "项目编号", required = true, dataType = "String"),
    })
    @PostMapping("findByMgrId")
    private @ResponseBody
    Response findByMgrId(
            @RequestParam("mgrId") String mgrId
    ) {
        SmsMgrInfo smsMgrInfo=smsBusiService.findByMgrId(mgrId);
        log.info(" 查询单个项目信息：{}", mgrId);
        return ResponseUtil.sucess(smsMgrInfo);
    }


}
